package util;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import Client.TorrentConverter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author d_frEak
 */
public class helper {

    public static String stringToHash(String teks) {
        //mengembalikan nilai hash teks
        //inisiaiisasi algoritma sha1
        MessageDigest md = null;

        try {
            md = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(TorrentConverter.class.getName()).log(Level.SEVERE, null, ex);
        }

        byte[] output;
        //hash nama file
        md.update(teks.getBytes());
        output = md.digest();
        return bytesToHex(output);
    }

    public static String bytesToHex(byte[] b) {
        char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        StringBuffer buf = new StringBuffer();
        for (int j = 0; j < b.length; j++) {
            buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
            buf.append(hexDigit[b[j] & 0x0f]);
        }
        return buf.toString();
    }

    

}
